import { downloadFileFromBlobPart } from '@vben/utils'

import { requestClient } from '#/api/request'

/**
 * 获取物料采购列表
 */
export async function getPurchaseList(params: any) {
  return requestClient.get('/admin/Purchase/index', params)
}

/**
 * 获取物料采购详情
 */
export async function getPurchaseDetailList(params: any) {
  return requestClient.post('/admin/purchase/getPurchaseDetail', params)
}

/**
 * 采购添加
 */
export async function doPurchaseAdd(params: any) {
  return requestClient.post('/admin/purchase/add', params)
}

/**
 * 采购编辑
 */
export async function doPurchaseEdit(params: any) {
  return requestClient.post('/admin/purchase/edit', params)
}

/**
 * 采购删除
 */
export async function doPurchaseDelete(params: any) {
  return requestClient.delete('/admin/purchase/del', params)
}

/**
 * 采购导出合同
 */
export async function doPurchaseExportContract(params: any) {
  return requestClient.post('/admin/purchase/daochu_contract_merge', params)
}

/**
 * 获取物料采购bom详情
 */
export async function getPurchaseDetailBomList(params: any) {
  return requestClient.post('/admin/purchase/getPurchaseBomList', params)
}

/**
 * 获取物料采购详情
 */
export async function getPurchaseDetailEditInfo(params: any) {
  return requestClient.get('/admin/purchase/edit', params)
}

/**
 * 获取采购订单
 */
export async function getPurchaseOrderList(params: any) {
  return requestClient.get('/admin/ProductOrder/index', params)
}

/**
 * 获取供应商
 */
export async function getSupplyList(params: any) {
  return requestClient.get('/admin/Supply/index', params)
}

/**
 * 获取供应商
 */
export async function getSupplyList2(params: any) {
  return requestClient.post('/admin/Supply/index', params)
}

/**
 * 获取公司列表
 */
export async function getCompanyList(params: any) {
  return requestClient.get('/admin/purchase/getCompanyList', params)
}

/**
 * 获取物料sku列表
 */
export async function getMaterialSkuList(params: any) {
  return requestClient.get('/admin/material/getMaterialSku', params)
}

/**
 * 审核
 */
export async function doPurchaseApproval(params: any) {
  return requestClient.post('/admin/purchase/approval', params)
}

/**
 * 反审
 */
export async function doPurchaseAgainst(params: any) {
  return requestClient.post('/admin/purchase/against', params)
}

/**
 * 标记合同
 */
export async function doMarkContractNew(params: any) {
  return requestClient.post('/admin/purchase/mark_contract_new', params)
}

/**
 * 财务标记
 */
export async function doSign(params: any) {
  return requestClient.post('/admin/purchase/sign', params)
}

/**
 * 采购明细
 */
export async function getPurchaseBomDetailList(params: any) {
  return requestClient.get('/admin/PurchaseBomDetail/index', params)
}

/**
 * 采购明细-导出
 */
/**
 * 采购明细-导出
 */
export async function exportExpiredData2(params: any) {
  await requestClient
    .download<Blob>('/admin/purchaseBomDetail/exportExpiredData', {
      ...params,
      responseType: 'blob', // 设置响应类型为 blob
      responseReturn: 'raw', // 返回原始响应而不是解析后的数据
    })
    .then((res) => {
      // downloadResult.value = `获取Blob成功，长度：${res.size}`;
      console.log('长度' + res.size)
    })

  // res 已经是 Blob 对象了，不需要额外处理
  // downloadFileFromBlobPart({
  //   source: res,
  //   fileName: '生产面辅料逾期情况.xlsx',
  // });
}

/**
 * 采购明细-导出
 */
export async function exportExpiredData(params: any) {
  const res = await requestClient.get('/admin/purchaseBomDetail/exportExpiredData', params, {
    responseType: 'blob', // 设置响应类型为 blob
    responseReturn: 'raw', // 返回原始响应而不是解析后的数据
  })

  const blob = new Blob([res.data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  })
  downloadFileFromBlobPart({
    source: blob,
    fileName: '生产面辅料逾期情况.xlsx',
  })
}

/**
 * 标记
 * @param params
 */
export async function doSignFinish(params: any) {
  return requestClient.post('/admin/PurchaseBomDetail/signFinish', params)
}
/**
 * 收料记录
 */
export async function getRevicedLog(params: any) {
  return requestClient.post('/admin/PurchaseBomDetail/getRevicedLog', params)
}

/**
 * 获取采购BOM列表
 */
export async function getPurchaseBomList(params: any) {
  return requestClient.post('/admin/purchase/getPurchaseBomList', params)
}



/**
 * 批量提交物料采购单修
 */
export async function batchSubmitApproval(params: any) {
  return requestClient.post('/admin/v2.UpdateDataTask/batchSubmitApproval', params,{responseReturn:'raw'})
}
